package com.powermanager.activity;

import android.animation.TimeAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.powermanager.R;
import com.powermanager.adapter.TimerAdapter;
import com.powermanager.bean.LineBean;
import com.powermanager.bean.TimeBean;
import com.powermanager.util.ControlUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.DatePicker;
import cn.qqtheme.framework.picker.TimePicker;

/**
 * Created by lqy on 16/7/4.
 */
public class TimerActivity extends BaseActivity implements TimerAdapter.OnItemClickeListenenr{
    @Bind(R.id.timer_relist)
    RecyclerView mRvDisplay;
    @Bind(R.id.mTvAdd)
    TextView mTvAdd;
    private int padding;
    private int textSize;
    private int itemSpace;
    private TimePicker timePicker;
    private DatePicker datePicker;
    private RecyclerView recyclerView;
    private TimerAdapter mAdapter;
    private List<TimeBean> timeBeans = new ArrayList<>();
    private ControlUtil controlUtil;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer);
        ButterKnife.bind(this);
        controlUtil = ControlUtil.getInstance();
        mRvDisplay.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TimerAdapter(this, timeBeans);
        mAdapter.setOnItemClickeListenenr(this);
        mRvDisplay.setAdapter(mAdapter);
        timePicker = new TimePicker(this, TimePicker.HOUR_OF_DAY);
        timePicker.setTopLineVisible(false);
        timePicker.setOnTimePickListener(new TimePicker.OnTimePickListener() {
            @Override
            public void onTimePicked(String hour, String minute, String second) {
                Toast.makeText(TimerActivity.this, hour + ":" + minute + ":" + second, Toast.LENGTH_SHORT).show();
            }
        });
        datePicker = new DatePicker(this, DatePicker.YEAR_MONTH_DAY);
        datePicker.setRange(1990, 2015);//年份范围
        datePicker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                Toast.makeText(TimerActivity.this, year + ":" + month + day, Toast.LENGTH_SHORT).show();
            }
        });
//        datePicker.show();

    }

    public void test(View view) {
        //默认选中当前时间

        timePicker.show();

    }

    @OnClick(R.id.mTvAdd)
    public void onClick() {
        Intent intent = new Intent(TimerActivity.this,AddTimerActivity.class);
        startActivityForResult(intent,1001);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK && requestCode == 1001){
            TimeBean timeBean = data.getParcelableExtra("data");
            timeBeans.add(timeBean);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onItemClicked(LineBean lineBean, boolean isChecked, String starthour, String startminute, String endHour, String endMinute) {
        byte[] bytes = new byte[22];
        bytes[0] = (byte) 0xAA;
        bytes[1] = (byte) 0xAA;
        bytes[2] = 0x01;
        bytes[3] = 0x00;
        bytes[4] = (byte) 0xBB;
        bytes[5] = (byte) 0xBB;
        bytes[6] = (byte) 0xCB;
        bytes[7] = (byte) 0xFE;
        bytes[8] = (byte) 0xFE;
        bytes[9] = (byte) (lineBean.getTag());
        bytes[10] = 0x13;
        bytes[11] = 0x00;
        bytes[12] = 0x05;
        bytes[13] = 0x07;
        int start_hour = Integer.valueOf(starthour);
        bytes[14] = (byte) start_hour;
        int start_minute = Integer.valueOf(startminute);
        bytes[15] = (byte) start_minute;
        int end_hour = Integer.valueOf(endHour);
        bytes[16] = (byte) end_hour;
        int end_minute = Integer.valueOf(endMinute);
        bytes[17] = (byte) end_minute;

        int sum = bytes[9] + bytes[10] + bytes[11] + bytes[12] + bytes[13]+bytes[14]+bytes[15]+bytes[16]+bytes[17];
        byte[] result = controlUtil.intToByteArray(sum);
        bytes[18] = result[1];
        bytes[19] = result[0];
        bytes[20] = (byte) 0xFD;
        bytes[21] = (byte) 0xFD;
        String re = "";
        for(int i = 0;i<bytes.length;i++){
            int b = bytes[i] & 0xFF;
            String v = Integer.toHexString(b);
            if(v.length()<2){
                v = "0"+v;
            }
            re   = re +v;
        }
        Log.d("command--->",re);

        controlUtil.sendData(bytes);
    }
}
